from django.shortcuts import *
from demo.models import *
import datetime
from django.db import connection
# Create your views here.
def login(request):
    if request.method == 'GET':
        return render(request,"login.html")
    else:
        uname = request.POST.get("uname")
        pwd = request.POST.get("pwd")
        userlist = user.objects.filter(uname=uname,pwd=pwd).first()
        if userlist:
            return redirect('show')
        else:
            return HttpResponse("密码或者账号错误")


def zhuce(request):
    if request.method == 'GET':
        return render(request,"zhuce.html")
    else:
        uname = request.POST.get("uname")
        pwd = request.POST.get("pwd")
        sex = request.POST.get("sex")
        tel = request.POST.get("tel")
        userlist = user.objects.filter(uname=uname, pwd=pwd,sex=sex,tel=tel).first()
        if userlist:
            return HttpResponse("账号已存在")
        else:
            user.objects.create(uname=uname, pwd=pwd,sex=sex,tel=tel)
            return redirect('login')


def show(request):
    ticketslist = tickets.objects.all()
    if request.method == "GET":
        return render(request,'show.html',{
            "ticketslist":ticketslist
        })
    else:
        tname = request.POST.get("tname")
        starttime = request.POST.get("starttime")
        endtime = request.POST.get("endtime")
        # starttime = starttime.replace('-','/')
        # endtime = endtime.replace('-','/')
        # print(starttime, endtime)
        # print(type(starttime))
        # ticketslist = tickets.objects.filter(tname__contains=tname).first()
        # starttime = datetime.datetime.strptime(starttime, '%Y-%m-%d')
        # endtime = datetime.datetime.strptime(endtime, '%Y-%m-%d')
        cursor = connection.cursor()
        # ttt = tickets.objects.all()
        # for t in ttt:
        #     print(type(t.begintime))
        sql = cursor.execute("""SELECT * FROM demo_tickets WHERE begintime BETWEEN'{}' AND '{}' and tname like '%{}%';""".format(starttime,endtime,tname))
        # cursor = connection.cursor()
        # ticketlist = tickets.objects.raw(sql)
        ticketlist = cursor.fetchall()
        print(ticketlist)
        for i in ticketlist:
            print(i[1])
        return render(request, 'show.html', {
            "ticketlist": ticketlist
        })
        # ticketslist = []
        # for ticket in ticketlist:
        #     if "冰球" in ticket.tname:
        #         ticketslist.append(ticket)
        # print(ticketslist)
        # print(ticketslist)
        # for i in ticketslist:
        #     print(i)

